/**
 * 
 */
package com.depot.basedata.companyInfo.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;


import com.depot.basedata.companyInfo.domain.TcCompany;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;
import com.soar.jdbc.dbhelper.DbParameter;
import com.soar.jdbc.dbhelper.DbTemplates;
import com.soar.jdbc.dbhelper.SameTypes;
import com.soar.jdbc.dbhelper.SqlTypes;
import com.soar.jdbc.dbhelper.WhereTypes;
import com.soar.jdbc.keyutils.KeyGenerator;
import com.soar.util.StringUtils;

public class TcCompanyDao
{
	/**
	 * @param env
	 * @param domain
	 * @param action
	 * @return
	 * @throws SQLException
	 */
	public static List<HashMap<String, String>> select(Environment env, TcCompany domain, BaseActionSupport action) throws SQLException
	{
		DbParameter param = new DbParameter("TC_COMPANY T1");
		StringBuilder sb = new StringBuilder();
		sb.append("T1.COMP_CODE,");
		sb.append("T1.COMP_NAME,");
		sb.append("T1.COMP_ADDRESS,");
		sb.append("T1.LINK_PERSON,");
		sb.append("T1.LINK_PHONE,");
		sb.append("T1.RECORD_STATUS");
		param.setSelectFields(sb.toString());
		return DbTemplates.select(env.getConnection(), param, action);
	}

	/**
	 * @param env
	 * @param domain
	 * @return
	 * @throws SQLException
	 */
	public static String doSave(Environment env, TcCompany domain) throws SQLException
	{
		DbParameter param = null;
		if (StringUtils.isEmpty(domain.getCompCode()))
		{// ID为空 新增
			param = new DbParameter("TC_COMPANY T1");
			param.setParams("COMP_CODE", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.VARCHAR);
			param.setParams("COMP_NAME", domain.getCompName(), SqlTypes.VARCHAR);
			param.setParams("COMP_TYPE", domain.getCompType(), SqlTypes.VARCHAR);
			param.setParams("IS_YJ", domain.getIsYj(), SqlTypes.VARCHAR);
			param.setParams("COMP_ADDRESS", domain.getCompAddress(), SqlTypes.VARCHAR);
			param.setParams("LINK_PERSON", domain.getLinkPerson(), SqlTypes.VARCHAR);
			param.setParams("LINK_PHONE", domain.getLinkPhone(), SqlTypes.VARCHAR);
			param.setParams("RECORD_STATUS", domain.getRecordStatus(), SqlTypes.VARCHAR);
			param.setParams("INPUT_USER", domain.getInputUser(), SqlTypes.VARCHAR);
			param.setParams("INPUT_TIME", domain.getInputTime(), SqlTypes.TIMESTAMP);
			if (DbTemplates.insert(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
		else
		{// ID不为空 修改
			param = new DbParameter("TC_COMPANY T1");
			param.setParams("COMP_CODE", KeyGenerator.getInstance().getKeyId(env.getConnection()), SqlTypes.VARCHAR);
			param.setParams("COMP_NAME", domain.getCompName(), SqlTypes.VARCHAR);
			param.setParams("COMP_TYPE", domain.getCompType(), SqlTypes.VARCHAR);
			param.setParams("IS_YJ", domain.getIsYj(), SqlTypes.VARCHAR);
			param.setParams("COMP_ADDRESS", domain.getCompAddress(), SqlTypes.VARCHAR);
			param.setParams("LINK_PERSON", domain.getLinkPerson(), SqlTypes.VARCHAR);
			param.setParams("LINK_PHONE", domain.getLinkPhone(), SqlTypes.VARCHAR);
			param.setParams("RECORD_STATUS", domain.getRecordStatus(), SqlTypes.VARCHAR);
			param.setParams("INPUT_USER", domain.getInputUser(), SqlTypes.VARCHAR);
			param.setParams("INPUT_TIME", domain.getInputTime(), SqlTypes.TIMESTAMP);
			if (DbTemplates.update(env.getConnection(), param))
			{
				return "1";
			}
			else
			{
				return "0";
			}
		}
	}

	public static HashMap<String, String> getCompanyById(Environment env, TcCompany domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("TC_COMPANY");
		param.setWhereParams("COMP_CODE", domain.getCompCode(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		return DbTemplates.select(env.getConnection(), param).get(0);
	}

	public static String delCompany(Environment env, TcCompany domain) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("TC_COMPANY");
		param.setWhereParams("COMP_CODE", domain.getCompCode(), SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		if (DbTemplates.delete(env.getConnection(), param))
		{
			return "1";
		}
		else
		{
			return "0";
		}

	}

	public static List<HashMap<String, String>> seletCompayType(Environment env, String fcodeid, String indexid) throws SQLException
	{
		DbParameter param = null;
		param = new DbParameter("SYSTEM_TCODE");
		param.setWhereParams("FCODEID", fcodeid, SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.YES);
		param.setWhereParams("FCODEINDEX", indexid, SqlTypes.VARCHAR, WhereTypes.AND, SameTypes.NO);
		List<HashMap<String, String>> list = DbTemplates.select(env.getConnection(), param);
		return list;
	}
}